/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvListedepartement;
import fr.cggveritas.crewintranet.jms.entities.CvMission;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvMissionJpaController1 extends DaoImp implements Serializable {

    public CvMissionJpaController1() {
        super();
    }

    public void create(CvMission cvMission) throws PreexistingEntityException, Exception {
        if (cvMission.getCvListedepartementList() == null) {
            cvMission.setCvListedepartementList(new ArrayList<CvListedepartement>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvListedepartement> attachedCvListedepartementList = new ArrayList<CvListedepartement>();
            for (CvListedepartement cvListedepartementListCvListedepartementToAttach : cvMission.getCvListedepartementList()) {
                cvListedepartementListCvListedepartementToAttach = em.getReference(cvListedepartementListCvListedepartementToAttach.getClass(), cvListedepartementListCvListedepartementToAttach.getIddepartement());
                attachedCvListedepartementList.add(cvListedepartementListCvListedepartementToAttach);
            }
            cvMission.setCvListedepartementList(attachedCvListedepartementList);
            em.persist(cvMission);
            for (CvListedepartement cvListedepartementListCvListedepartement : cvMission.getCvListedepartementList()) {
                CvMission oldIdmissionOfCvListedepartementListCvListedepartement = cvListedepartementListCvListedepartement.getIdmission();
                cvListedepartementListCvListedepartement.setIdmission(cvMission);
                cvListedepartementListCvListedepartement = em.merge(cvListedepartementListCvListedepartement);
                if (oldIdmissionOfCvListedepartementListCvListedepartement != null) {
                    oldIdmissionOfCvListedepartementListCvListedepartement.getCvListedepartementList().remove(cvListedepartementListCvListedepartement);
                    oldIdmissionOfCvListedepartementListCvListedepartement = em.merge(oldIdmissionOfCvListedepartementListCvListedepartement);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvMission(cvMission.getIdmission()) != null) {
                throw new PreexistingEntityException("CvMission " + cvMission + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvMission cvMission) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvMission persistentCvMission = em.find(CvMission.class, cvMission.getIdmission());
            List<CvListedepartement> cvListedepartementListOld = persistentCvMission.getCvListedepartementList();
            List<CvListedepartement> cvListedepartementListNew = cvMission.getCvListedepartementList();
            List<String> illegalOrphanMessages = null;
            for (CvListedepartement cvListedepartementListOldCvListedepartement : cvListedepartementListOld) {
                if (!cvListedepartementListNew.contains(cvListedepartementListOldCvListedepartement)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvListedepartement " + cvListedepartementListOldCvListedepartement + " since its idmission field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CvListedepartement> attachedCvListedepartementListNew = new ArrayList<CvListedepartement>();
            for (CvListedepartement cvListedepartementListNewCvListedepartementToAttach : cvListedepartementListNew) {
                cvListedepartementListNewCvListedepartementToAttach = em.getReference(cvListedepartementListNewCvListedepartementToAttach.getClass(), cvListedepartementListNewCvListedepartementToAttach.getIddepartement());
                attachedCvListedepartementListNew.add(cvListedepartementListNewCvListedepartementToAttach);
            }
            cvListedepartementListNew = attachedCvListedepartementListNew;
            cvMission.setCvListedepartementList(cvListedepartementListNew);
            cvMission = em.merge(cvMission);
            for (CvListedepartement cvListedepartementListNewCvListedepartement : cvListedepartementListNew) {
                if (!cvListedepartementListOld.contains(cvListedepartementListNewCvListedepartement)) {
                    CvMission oldIdmissionOfCvListedepartementListNewCvListedepartement = cvListedepartementListNewCvListedepartement.getIdmission();
                    cvListedepartementListNewCvListedepartement.setIdmission(cvMission);
                    cvListedepartementListNewCvListedepartement = em.merge(cvListedepartementListNewCvListedepartement);
                    if (oldIdmissionOfCvListedepartementListNewCvListedepartement != null && !oldIdmissionOfCvListedepartementListNewCvListedepartement.equals(cvMission)) {
                        oldIdmissionOfCvListedepartementListNewCvListedepartement.getCvListedepartementList().remove(cvListedepartementListNewCvListedepartement);
                        oldIdmissionOfCvListedepartementListNewCvListedepartement = em.merge(oldIdmissionOfCvListedepartementListNewCvListedepartement);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cvMission.getIdmission();
                if (findCvMission(id) == null) {
                    throw new NonexistentEntityException("The cvMission with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvMission cvMission;
            try {
                cvMission = em.getReference(CvMission.class, id);
                cvMission.getIdmission();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvMission with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvListedepartement> cvListedepartementListOrphanCheck = cvMission.getCvListedepartementList();
            for (CvListedepartement cvListedepartementListOrphanCheckCvListedepartement : cvListedepartementListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvMission (" + cvMission + ") cannot be destroyed since the CvListedepartement " + cvListedepartementListOrphanCheckCvListedepartement + " in its cvListedepartementList field has a non-nullable idmission field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cvMission);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvMission> findCvMissionEntities() {
        return findCvMissionEntities(true, -1, -1);
    }

    public List<CvMission> findCvMissionEntities(int maxResults, int firstResult) {
        return findCvMissionEntities(false, maxResults, firstResult);
    }

    private List<CvMission> findCvMissionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvMission.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvMission findCvMission(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvMission.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvMissionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvMission> rt = cq.from(CvMission.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
